{include file="public/head"}

<body>
<div id="wrapper">

    <div class="gray-bg dashbard-1">
        <div class="wrapper wrapper-content animated fadeInRight">
            <div class="row">
                <div class="col-lg-12">
                    <div class="ibox float-e-margins">
                        <div class="ibox-content">
                            <form class="form-horizontal m-t" id="commentForm">
                                <div class="form-group">
                                    <label class="col-sm-3 control-label">分类名称：</label>
                                    <div class="col-sm-8">
                                        <input id="name" name="name" maxlength="50" type="text" class="form-control" required="" aria-required="true">
                                    </div>
                                </div>
                                <div class="form-group">
                                    <label class="col-sm-3 control-label">所属分类：</label>
                                    <div class="col-sm-8">
                                        <select class="form-control m-b" id="parent_id">
                                            <option value="0">顶级分类</option>
                                            {volist name="ParentClass" id="v"}
                                            <option value="{$v.id}">{$v.name}</option>
                                            {/volist}
                                        </select>
                                    </div>
                                </div>
                                <div class="form-group">
                                    <label class="col-sm-3 control-label">分类图片：</label>
                                    <div class="col-sm-8">
                                        <label for="img_url">
                                            <img id="m_img_url" src="__STATIC__/Admin/img/no_img.png" style="max-width:  100%">
                                            <input type="file" id="img_url" class="upload_pic" style="display: none;">
                                        </label>
                                        <span class="help-block m-b-none"><i class="fa fa-info-circle"></i> 图片尺寸：父级分类1920*700 子级分类100*100</span>
                                    </div>
                                </div>
                                <div class="form-group">
                                    <label class="col-sm-3 control-label">是否推荐：</label>
                                    <div class="col-sm-8">
                                        <select class="form-control m-b" id="is_recommend">
                                            <option value="0">否</option>
                                            <option value="1">是</option>
                                        </select>
                                    </div>
                                </div>
                                <div class="form-group">
                                    <div class="col-sm-4 col-sm-offset-3">
                                        <button class="btn btn-primary" type="submit">确认添加</button>
                                    </div>
                                </div>
                            </form>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>


</div>
{include file="public/footer"}
<script>
    //ajax异步图片上传
    $('.upload_pic').change(function(){
        //图片对象
        let that = $(this);
        var formData = new FormData();
        formData.append("image", that[0].files[0]);

        // 上传设置
        $.ajax({
            // 规定把请求发送到那个URL
            url: "{:url('Goods/forUpload')}",
            type: "post",            // 请求方式
            dtaType: 'json',
            data: formData,//传值就传fd对象了
            processData:false,  //告诉jQuery不要处理数据
            contentType: false,  //告诉jQuery不要设置contentType
            // 请求成功时执行的回调函数
            success: function (data) {
                if(data.status == 1)
                {
                    that.prev().attr('src',data.message);
                }
                else
                {
                    alert(data.message);
                }
            }
        });
    });

    $('form').submit(function (e) {
        e.preventDefault();
        $.ajax({
            type:"post",
            url:"{:url('Goods/ClassAdd')}",
            data: {
                img_url:$('#m_img_url').attr('src'),
                name:$('#name').val(),
                parent_id:$('#parent_id').val(),
                is_recommend:$('#is_recommend').val(),
            },
            dataType:"json",
            success:function(data){
                if(data.status == 1){
                    layer.msg(data.message,{icon: 6,time:1500},function () {
                        window.top.location.reload();
                    });
                }else{
                    layer.msg(data.message,{icon: 5,time:1500});
                }
            }
        })
    })
</script>
</body>
</html>
